/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   LegalSystemServiceImpl.java for com.pengtu.gsj.service.impl.check.zhgl.industrymanage    </p>
 * 
 * <p>最后修改 : $: 2017年8月16日-下午8:28:45 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.zhgl.industrymanage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.zhgl.industrymanage.LegalSystemService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * LegalSystemServiceImpl
 * 
 * 2017年8月16日 下午8:28:45
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class LegalSystemServiceImpl implements LegalSystemService {
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	
	@Override
	public Map<String, Object> getLegalSystemMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		double firstStepScore = Double.parseDouble((String)jsonObject.get("firstStepScore"));
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//获取政府规章
		List<Attachment> zfgzList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "fzjs_zfgz");
		//获取省级规范性文件
		List<Attachment> fxwjList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "fzjs_gfxwj");
		//获取省级规范性文件
		List<Attachment> sjbmList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "fzjs_sjbm");
		
		double [] arg ={yearFull, firstStepScore, poitMark};
		
		double total =0; 								//检查得分
		double percentage =0; 							//系统完善度
		double tableFull  = this.bubbleSort(arg)[0];	//满分
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 1)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		
		if (zfgzList.size() > 0) {
			total = yearFull;
		} else if (fxwjList.size() > 0) {
			total = fxwjList.size()*yearPart;
			total = total > poitMark ? poitMark :total;
		} else if (sjbmList.size() > 0) {
			total = sjbmList.size()*reserveMark;
			total = total > firstStepScore ? firstStepScore :total;
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		map.put("percent", percentage);
		map.put("years", years);
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
	
	
	/**
	 * 
	 * bubbleSort:
	 * 适用:冒泡排序算法 降序
	 * @param args
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public double[] bubbleSort(double[] args){
		for(int i=0;i<args.length-1;i++){   
			for (int j=i+1;j<args.length;j++) {   
				if (args[i] < args[j]) {   
					double temp=args[i];   
                    args[i]=args[j];   
                    args[j]=temp;   
                }   
            }  
       }  
       return args;  
	} 
}
